<?php

namespace App\Console\Workerman;

use App\Models\SiteCate;
use App\Models\SiteWechat;
use App\Service\Curl;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
use QL\QueryList;

use Workerman\Worker;

class _Workerman extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'Workerman {action} {--daemonize}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        global $argv;//定义全局变量
        $arg     = $this->argument('action');
        $argv[1] = $arg;
        $argv[2] = $this->option('daemonize') ? '-d' : '';//该参数是以daemon（守护进程）方式启动

        global $text_worker;
        // 创建一个Worker监听2347端口，使用websocket协议通讯
        $text_worker                 = new Worker("websocket://0.0.0.0:2347");
        $text_worker->uidConnections = array();//在线用户连接对象
        $text_worker->uidInfo        = array();//在线用户的用户信息
        // 启动1个进程对外提供服务，这里设置多个进程在服务端发送客户端时会有问题
        $text_worker->count = 1;
        //当启动workerman的时候 触发此方法
        $text_worker->onWorkerStart = function() {

        };
        //当浏览器连接的时候触发此函数
        $text_worker->onConnect = function($connection) {

        };
        //向用户发送信息的时候触发
        //$connection 当前连接的人的信息 $data 发送的数据
        $text_worker->onMessage = function($connection, $data) {

        };
        //浏览器断开链接的时候触发
        $text_worker->onClose = function($connection) { };
        Worker::runAll();
    }

}

